package com.ruoyi.project.bid.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.bid.domain.Wechat;
import com.ruoyi.project.bid.domain.vo.CustomerVO;
import com.ruoyi.project.bid.service.IWechatService;
import com.ruoyi.project.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.bid.domain.Customer;
import com.ruoyi.project.bid.service.ICustomerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
//import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 客户管理Controller
 * 
 * @author ruoyi
 * @date 2023-11-11
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/business/customer")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IWechatService wechatService;

    /**
     * 查询客户列表
     */
    @ApiOperation("客户列表")
//    @PreAuthorize("@ss.hasPermi('business:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerVO customerVO)
    {
        startPage();

        if (!SecurityUtils.isComAdmin()) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SysUser user = loginUser.getUser();
            String department = user.getDepartment();
            Wechat wechat = new Wechat();
            // 拓客部
            if ("2".equals(department)) {
                wechat.setTokerId(user.getUserId());
            } else if ("3".equals(department)) {
                // 业务部
                wechat.setBusinessId(user.getUserId());
            }

            List<Wechat> list = wechatService.selectWechatList(wechat);
            if (!list.isEmpty()) {
                List<String> ids = list.stream().map(Wechat::getWxId).collect(Collectors.toList());
                customerVO.setWxIds(ids);
            } else {
                return getDataTable(new ArrayList<>());
            }
        }

        List<Customer> list = customerService.selectCustomerList(customerVO);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('business:customer:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Customer customer)
    {
//        List<Customer> list = customerService.selectCustomerList(customer);
//        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
//        return util.exportExcel(list, "customer");
        return null;
    }

    /**
     * 获取客户详细信息
     */
    @ApiOperation("客户详情")
//    @PreAuthorize("@ss.hasPermi('business:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerService.selectCustomerById(id));
    }

    /**
     * 新增客户
     */
    @ApiOperation("新增客户")
//    @PreAuthorize("@ss.hasPermi('business:customer:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Customer customer)
    {
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @ApiOperation("修改客户")
//    @PreAuthorize("@ss.hasPermi('business:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Customer customer)
    {
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户
     */
    @ApiOperation("删除客户")
//    @PreAuthorize("@ss.hasPermi('business:customer:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerService.deleteCustomerByIds(ids));
    }
}
